<?php

/**
 #########################
 * Writen by John Harman 14 Mar 2005, for Infotex UK Limited, copyright Infotex UK Limited 2005
 * Function to return DB login details, these can either be hard coded below, or imported from
 * within the inc_dbdetails.php script (recommended)
 * moved to inc_funcs Nov 05 as also used for many other scripts besides shopcart
 * @return array
 #########################
*/
function dbdetails() {
  if (file_exists("inc_dbdetails.php")) {
    include ("inc_dbdetails.php");
    return $dbdetails;
  } else {
    if (file_exists("includes/inc_dbdetails.php")) {
      include ("includes/inc_dbdetails.php");
      return $dbdetails;
    } else {
    //format=user, password, dbname, server
      $dbdetails=array("test", "test", "test", "localhost");
    };
  };
  return $dbdetails;
};



/**
 #########################
* abc
 #########################
*/
function getFactors($number) {
	$returnval=array();
	$c=0;

	for($y = 1; $y <= $number; $y++) {
    $z = $number % $y;
    if($z == 0) {
      $returnval[$c]=$y;
      $c++;
    }
  }
  return $returnval;
}

/**
 #########################
* abc
 #########################
*/
function savereplay($title,$keywords,$description,$filename,$player_numbers,$player_names,$player_races,$player_colours,$map,$length,$speed,$mode,$realm,$winner,$version,$date_played) {
  if (function_exists("dbdetails")==FALSE) {
    die ("<p>ERROR: requires dbdetails function call</p>");
  };

  $arydbdetails=dbdetails();
  $db=mysql_connect($arydbdetails[3],$arydbdetails[0],$arydbdetails[1]);
  @mysql_select_db($arydbdetails[2],$db) or die( "<p>ERROR: Unable to open database, please check details</p>");

  $retval='';
  $strsel="INSERT INTO replays(title,keywords,description,up,down,filename,player_numbers,player_names,player_races,player_colours,map,length,speed,mode,realm,winner,version,date_played) 
           VALUES ('".$title."','".$keywords."','".$description."',0,0,'".$filename."','".$player_numbers."','".$player_names."','".$player_races."','".$player_colours."','".$map."','".$length."','".$speed."','".$mode."','".$realm."','".$winner."','".$version."','".date("Y.m.d H:i:s")."');";
           $retval.=$strsel;
  $rsresult=mysql_query($strsel,$db);
  if ($rsresult==TRUE)
    $retval.="a";
  else
    $retval.="b";
  return $retval;
}


/**
 #########################
* abc
 #########################
*/
function showreplays($number=10,$boxno) {
  if (function_exists("dbdetails")==FALSE) {
    die ("<p>ERROR: requires dbdetails function call</p>");
  };

  $arydbdetails=dbdetails();
  $db=mysql_connect($arydbdetails[3],$arydbdetails[0],$arydbdetails[1]);
  @mysql_select_db($arydbdetails[2],$db) or die( "<p>ERROR: Unable to open database, please check details</p>");

  $retval.="";
  //$master=0;
  //while($master<3) {
    switch ($boxno) {
    case 0:
        $strsel="SELECT *, (up-down) AS score FROM replays WHERE player_race_1 = 'Protoss' OR player_race_2 = 'Protoss' ORDER BY score DESC LIMIT ".$number.";";
        $race_search="Protoss";
        break;
    case 1:
        $strsel="SELECT *, (up-down) AS score FROM replays WHERE player_race_1 = 'Zerg' OR player_race_2 = 'Zerg' ORDER BY score DESC LIMIT ".$number.";";
        $race_search="Zerg";
        break;
    case 2:
        $strsel="SELECT *, (up-down) AS score FROM replays WHERE player_race_1 = 'Terran' OR player_race_2 = 'Terran' ORDER BY score DESC LIMIT ".$number.";";
        $race_search="Terran";
        break;
    case 3:
        $strsel="SELECT * FROM replays LIMIT ".$number." ORDER BY ;";
        break;
    case 4:
        $strsel="SELECT * FROM replays LIMIT ".$number." ORDER BY ;";
        break;
    }
    $rsresult=mysql_query($strsel,$db);

    $num=mysql_num_rows($rsresult);


    $retval.="<h1>Top Results for ".$race_search."</h1>
    <div id=\"replay_container\">
    <table cellpadding=\"0\" cellspacing=\"0\" class=\"data\" style=\"width:100%\">
      <tbody>
        <tr class=\"first\">
          <th width=\"3%\" class=\"table_titles_mid\">RATING</th>
          <th width=\"30%\" class=\"table_titles_left\">TITLE</th>
          <th width=\"25%\" class=\"table_titles_left\">KEYWORD</th>
          <th width=\"10%\" class=\"table_titles_mid\"></th>
          <th width=\"3%\" class=\"table_titles_mid\">PLAYERS</th>
          <th width=\"10%\" class=\"table_titles_mid\"></th>
          <th width=\"10%\" class=\"table_titles_mid\">MAP</th>
          <th width=\"8%\" class=\"table_titles_mid\">LENGTH</th>
          <th width=\"5%\"></ht>
          <th width=\"5%\"></ht>
        </tr>";
    for($i=0;$i<$num;$i++) {
      if(($i%2)==0)
      $colval="style=\"background:#f8f1f8;\"";
        else
      $colval="style=\"background:#f4f4f4;\"";
      $score=(mysql_result($rsresult,$i,"up")-(mysql_result($rsresult,$i,"down")));
      if($score>0)
        $div_id="score_up";
      else
        $div_id="score_down";
      $title=substr(mysql_result($rsresult,$i,"title"), 0, 50);
      $map=mysql_result($rsresult,$i,"map");
      //$length=mysql_result($rsresult,$i,"length");
      $length=explode(",",mysql_result($rsresult,$i,"length"));
      $realm=mysql_result($rsresult,$i,"realm");
      $keywords=explode("|",mysql_result($rsresult,$i,"keywords"));
      $k_words='';
      foreach($keywords as $keyword) {
        if($keyword!='')
          $k_words.="<span class=\"keywords\">".$keyword."</span>";
      }
      $names=explode("|",mysql_result($rsresult,$i,"player_names"));
      $name1=$names[0];
      $name2=$names[1];
 
      //$races=explode("|",mysql_result($rsresult,$i,"player_races"));
      $cols=explode("|",mysql_result($rsresult,$i,"player_colours"));
      //$races=explode("|",mysql_result($rsresult,$i,"player_races"));
      $race1=mysql_result($rsresult,$i,"player_race_1");
      $race2=mysql_result($rsresult,$i,"player_race_2");
      if($race_search==$race2) {
        $temp=$race2;
        $race2=$race1;
        $race1=$temp;
        $temp=$name2;
        $name2=$name1;
        $name1=$temp;
      }
      for($v=0;$v<2;$v++) {
        if($races[$v]=="Zerg")
          $raceimg[$v]="<img width=\"25px\" height=\"25px\" src=\"/images/zerg.png\" />";
        elseif($races[$v]=="Terran")
          $raceimg[$v]="<img width=\"25px\" height=\"25px\" src=\"/images/terran.png\" />";
        elseif($races[$v]=="Protoss")
          $raceimg[$v]="<img width=\"25px\" height=\"25px\" src=\"/images/protoss.png\" />";
      }

      $retval.="
        <tr class=\"table_row\" ".$colval.">
          <td class=\"".$div_id."\" width=\"3%\">".$score."</td>
          <td class=\"title\" width=\"30%\">".$title."</td>
          <td width=\"25%\">".$k_words."</td>
          <td width=\"10%\" class=\"name_1\">".$name1." <img width=\"20px\" height=\"20px\" src=/images/".$race1.".png /></td>
          <td width=\"3%\" class=\"vs\" width=\"1%\"> vs </td>
          <td width=\"10% \" class=\"name_2\"><img width=\"20px\" height=\"20px\" src=/images/".$race2.".png /> ".$name2."</td>
          <td width=\"10%\" class=\"map\">".$map."</td>
          <td width=\"8%\" class=\"length\">".$length[0]."</td>
          <td width=\"5%\"><span class=\"download\"><img width=\"20px\" height=\"20px\" src=\"/images/dl.png\" /></span></td>
          <td width=\"5%\"><span class=\"info\">i</span></td>
        </tr></div>
        ";
    }
    $retval.="</tbody></table></div>";

  return $retval;
}
?>